/*
 * Copyright (c) 2009-2010, Sergey Karakovskiy and Julian Togelius
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *  Redistributions of source code must retain the above copyright
 * notice, this list of conditions and the following disclaimer.
 *  Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 *  Neither the name of the Mario AI nor the
 * names of its contributors may be used to endorse or promote products
 * derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 * IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

package ch.idsia.scenarios.champ;

import ch.idsia.agents.Agent;
import ch.idsia.agents.CompNaturalTrainingMODEAgent;
import ch.idsia.agents.LearningAgent;
import ch.idsia.benchmark.tasks.BasicTask;
import ch.idsia.benchmark.tasks.CompNaturalMultObjectiveTask;
import ch.idsia.benchmark.tasks.GamePlayTask;
import ch.idsia.benchmark.tasks.Task;
import ch.idsia.tools.EvaluationInfo;
import ch.idsia.tools.MarioAIOptions;
import ch.idsia.utils.wox.serial.Easy;


public final class CompNaturalRunTreinedAgent
{
final static int numberOfLevels = 10;
private static int killsSum = 0;
private static float marioStatusSum = 0;
private static int timeLeftSum = 0;
private static int marioModeSum = 0;
private static boolean detailedStats = false;
private static MarioAIOptions marioAIOptions = new MarioAIOptions();

//public static void evaluateAgent(final Agent agent)
//{
//    final Task task = new GamePlayTask(marioAIOptions);
//    //marioAIOptions.setAgent(agent);
//    task.setOptionsAndReset(marioAIOptions);
//    System.out.println("Evaluating agent " + agent.getName() + " with seed " + marioAIOptions.getLevelRandSeed());
//    task.doEpisodes(numberOfLevels, false, 1);
//    task.printStatistics();
//}
public static void evaluateAgent(final Agent agent)
{
	marioAIOptions.setVisualization(true);
    System.out.println("LearningTrack best agent = " + agent);
    marioAIOptions.setAgent(agent);
    BasicTask basicTask = new CompNaturalMultObjectiveTask(marioAIOptions);
    basicTask.setOptionsAndReset(marioAIOptions);
    System.out.println("basicTask = " + basicTask);
    System.out.println("agent = " + agent);

    boolean verbose = true;

    if (!basicTask.runSingleEpisode(1))  // make evaluation on the same episode once
    {
        System.out.println("MarioAI: out of computational time per action! Agent disqualified!");
    }
    EvaluationInfo evaluationInfo = basicTask.getEvaluationInfo();
    System.out.println(evaluationInfo.toString());

    int f = evaluationInfo.computeWeightedFitness();
    if (verbose)
    {
        System.out.println("Intermediate SCORE = " + f + ";\n Details: " + evaluationInfo.toString());
    }
    
    for (int i = 1; i <= 2; i++) {
		generateRandLevel(marioAIOptions, i);
		basicTask.runSingleEpisode(1);
	}
    
}

public static void evaluateSubmissionZip(final String zipFileName)
{

}

private static MarioAIOptions generateRandLevel(MarioAIOptions ag, int i) {
	MarioAIOptions mario = new MarioAIOptions();
	ag.setLevelLength((200 + (i * 12) + (ag.getLevelRandSeed() % (i + 1))) % 512);
	ag.setLevelType(i % 3);
	ag.setLevelRandSeed(ag.getLevelRandSeed() + i);
	ag.setLevelDifficulty(i % 1);
	ag.setGapsCount(false);
	ag.setCannonsCount(false);
	ag.setCoinsCount(false);
	ag.setBlocksCount(true);
	ag.setHiddenBlocksCount(true);
	ag.setFrozenCreatures(false);
	ag.setEnemies(i % 2 == 1 ? "off" : "");
	
	ag.setVisualization(true);

	return ag;
}

public static void main(String[] args)
{
    marioAIOptions = new MarioAIOptions(args);
    Agent agent = (Agent) Easy.load("Resultados\\MODE evolved-progress-CompNaturalTrainingMODEAgentGeneration 1734 score 6973.3335-uid-2012-06-25_15-24-05.xml");
    marioAIOptions.setAgent(agent);
    marioAIOptions.setArgs("-tl 150 -z on -ld 1 -ls 133829");
    marioAIOptions.setCoinsCount(false);
    evaluateAgent(agent);
    
}
}


